//
//  ZJLiveController.m
//  Lottery
//
//  Created by ZJ on 2018/8/16.
//  Copyright © 2018年 iCatch Technology Inc. All rights reserved.
//

#import "ZJLiveController.h"
#import "UIView+ZJFrame.h"

@interface ZJLiveController ()

@property (nonatomic, weak) UIDatePicker *datePicker;

@end

@implementation ZJLiveController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 取消点击
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // 第一组不点
    if (indexPath.section == 0) {
        return;
    }
    
    // 创建一个看不见的文本框
    UITextField *text = [[UITextField alloc] init];
    
    // 获取 cell
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    // 添加到 cell 上
    [cell.contentView addSubview:text];
    
    // 创建 datePicker
    UIDatePicker *datePicker = [[UIDatePicker alloc] init];
    self.datePicker = datePicker;
    
    // 中文
    datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    
    // 时间格式
    datePicker.datePickerMode = UIDatePickerModeTime;
    
    // 设置文本框的 inputView
    text.inputView = datePicker;
    
    // 创建 toolbar
    UIToolbar *bar = [[UIToolbar alloc] init];
    // 设置告诉
    bar.height = 44;
    
    // item - 取消
    UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancelClick)];
    
    // item - 弹簧
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    // item - 完成
    UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(doneClick)];
    
    // 设置 toolbar 的 item
    bar.items = @[cancelItem, item, doneItem];
    
    // 设置文本框的 inputAccessoryView
    text.inputAccessoryView = bar;
    
    // 让文本框成为第一响应者
    [text becomeFirstResponder];
}

// 收键盘
- (void)cancelClick {
    [self.view endEditing:YES];
}

// 完成按钮点击事件
- (void)doneClick {
    // 获取 datePicker 的时间
    NSDate *date = self.datePicker.date;
    // 创建格式化时间的对象
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // 设置格式化时间对象的格式
    formatter.dateFormat = @"HH:mm";
    // 把date 转成 string
    NSString *time = [formatter stringFromDate:date];
    
    // 获取 indexPath
    NSIndexPath *path = [self.tableView indexPathForSelectedRow];
    
    // 获取 cell
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:path];
    
    // 修改时间
    cell.detailTextLabel.text = time;
    
    // 点取消
    [self cancelClick];
}

@end
